<?php

namespace app\model\goods;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-goods_find
 * Class GoodsFindModel
 * @package app\model
 */
class GoodsFindModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'goods_find';

    /**
     * 范围查询-order_status
     * @param $query
     * @param $status
     */
    public function scopeStatus($query, $status)
    {
        if ($status != null) {
            $query->where('a.order_status', '=', $status);
        }
    }


    /**
     * 范围查询-order_number
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('c.goods_title', '%' . trim($keyword) . '%');
        }
    }

    /**
     * 获取单条数据
     * @param $id
     * @param string $field
     * @param string $with
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id, $field = 'a.*', $with = '')
    {
        $data = $this
            ->alias("a")
            ->leftJoin("t_user b", "a.user_id=b.id")
            ->leftJoin("t_goods c", "a.goods_id=c.id")
            ->field($field)
            ->where('a.id', $id)
            ->find();
        return $data;
    }

    /**
     * 获取分页列表
     * @param $status
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getPageData($status, $keyword, $current_page, $list_rows, $field = 'a.*')
    {
        $dataList = $this
            ->alias("a")
            ->leftJoin("t_user b", "a.user_id=b.id")
            ->leftJoin("t_goods c", "a.goods_id=c.id")
            ->field($field)
            ->status($status)
            ->keyword($keyword)
            ->order('a.create_time', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);

        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

}
